#include "assoc.h"
#include<QString>
#include<QtAlgorithms>
#include <math.h>
#include<QVector>
#include<QSet>
#include<QList>

void Get_Dist(QVector<QVector<double> >& Termdoc,QVector<QVector<double> >&Assoc , QString& Assoc_Type)
{
    QVector<double> R;
    double a,b,c,d;
    for(int i1=0;i1<Termdoc.count();++i1)
    {
        R.clear();
      for(int i3=0;i3<Termdoc.count();++i3)
      {
          a=0;b=0;c=0;d=0;

        for(int i2=0;i2<Termdoc[1].count();++i2)
        {
            if(Termdoc[i1].value(i2)!=0 && Termdoc[i3].value(i2)!=0) ++a;
            if(Termdoc[i1].value(i2)!=0 && Termdoc[i3].value(i2)==0) ++b;
            if(Termdoc[i1].value(i2)==0 && Termdoc[i3].value(i2)!=0) ++c;
            if(Termdoc[i1].value(i2)==0 && Termdoc[i3].value(i2)==0) ++d;
        }
        if(Assoc_Type=="Jaccard")R.append(a/(a+b+c));
        if(Assoc_Type=="Simple matching")R.append((a+d)/(a+b+c+d));
        if(Assoc_Type=="Yule")R.append((a*d-b*c)/(a*d+b*c));
        if(Assoc_Type=="Hamann")R.append((a+d-b-c)/(a+d+b+c));
        if(Assoc_Type=="Sorenson")R.append(2*a/(2*a+b+c));
        if(Assoc_Type=="Rogers and Tanimoto")R.append((a+d)/(a+2*b+2*c+d));
        if(Assoc_Type=="Russell and Rao")R.append(a/(a+b+c+d));
        if(Assoc_Type=="Baroni-Urbani and Buser")R.append((a+sqrt(a*d))/(a+b+c+sqrt(a*d)));
        if(Assoc_Type=="Sokal binary distance")R.append(sqrt((b+c)/(a+b+c+d)));
        if(Assoc_Type=="Ochiai")R.append(a/sqrt((a+b)*(a+c)));
        if(Assoc_Type=="FirstSN")R.append((c+b)/(a+b+c+d));
        if(Assoc_Type=="SecondSN")R.append((c+b)/(a+b+c));
        if(Assoc_Type=="Soukal-Snis")R.append(a/(a+2*(b+c)));
        if(Assoc_Type=="First Kulchinsky")R.append(a/(b+c));
        if(Assoc_Type=="Second Kulchinsky")R.append(((a/(a+b))+(a/(a+c)))/2);
        if(Assoc_Type=="Overlap coefficient")R.append(a/qMin((a+c),(a+b)));

      }
      Assoc.append(R);
    }
}
void GetDistName(QList<QString>& Name)
{
    Name<<"Jaccard"<<"Simple matching"<<"Yule"<<"Hamann"<<"Sorenson"<<"Rogers and Tanimoto"<<"Russell and Rao"
          <<"Baroni-Urbani and Buser"<<"Sokal binary distance"<<"Ochiai"<<"FirstSN"<<"SecondSN"
            <<"Soukal-Snis"<<"First Kulchinsky"<<"Second Kulchinsky"<<"Overlap coefficient";
}
